import http from "@/utils/Http.ts";
import {Page} from "@/data/PageData.ts";
import {NodeData} from "@/data/TaskData.ts";

export interface TaskNamespace {
    id:string,
    name:string
}

export const taskNamespaceList = () => {
    return http<Array<TaskNamespace>>({
        url: '/task/taskNamespaceList',
        method: 'get'
    })
}

export const addNamespace = (name:string) => {
    return http<TaskNamespace>({
        url: '/task/addNamespace',
        method: 'post',
        data: { 'name' : name }
    })
}

export const deleteNamespace = (id:string) => {
    return http({
        url: '/task/deleteNamespace',
        method: 'delete',
        params: { id: id },
    })
}

export interface TaskData{
    id:string,
    name:string,
    namespace:string,
    lastStatus:string,
    lastTime:string,
    lastMsg:string,
    lastOperator:string,
    lastBranch:string
}

export const getTaskList = (namespace:string | null) => {
    return http<Page<TaskData>>({
        url: '/task/taskList',
        method: 'post',
        data: { 'namespace' : namespace, 'pageSize': 1000 }
    })
}

export interface AddTaskData{
    name:string,
    namespace:string,
    edit:boolean,
    id:string
}

export const addTask = (data: AddTaskData) => {
    return http({
        url: '/task/addTask',
        method: 'post',
        data: data
    })
}

export const deleteBatchTask = (list:Array<string>) => {
    return http({
        url: '/task/deleteTask',
        method: 'post',
        data: { "list" : list }
    })
}

export interface TaskInfo{
    id:string,
    name:string,
    namespace:string,
    namespaceRel:string,
    note:string,
    flowData:string,
    taskDataList:Array<NodeData>,
    status:string,
    createTime:string
}

export const getTaskInfo = (id:string, edit: boolean) => {
    return http<TaskInfo>({
        url: '/task/taskInfo',
        method: 'get',
        params: { id : id, edit: edit }
    })
}

export interface SaveTaskInfo{
    id:string,
    flowData:string,
    taskDataList:Array<NodeData>
}

export const saveFlowData = (data: SaveTaskInfo) => {
    return http({
        url: '/task/saveFlowData',
        method: 'post',
        data: data
    })
}

export const updateTaskData = (data: SaveTaskInfo) => {
    return http({
        url: '/task/updateTaskData',
        method: 'post',
        data: data
    })
}

export const runTask = (taskId:string) => {
    return http({
        url: '/task/runTask',
        method: 'get',
        params: { taskId : taskId }
    })
}

export const stopTask = (taskId:string) => {
    return http({
        url: '/task/stopTask',
        method: 'get',
        params: { taskId : taskId }
    })
}

export interface QueryHis{
    taskId:string,
    pageNum:number,
    pageSize:number
}

export interface TaskHisData{
    id: number,
    taskId: string,
    batchId: number,
    gitMsg: string,
    gitBranch: string,
    status: string,
    optUser: string,
    logFile: string,
    createTime:string
}

export const getTaskHisList = (data: QueryHis) => {
    return http<Page<TaskHisData>>({
        url: '/task/hisList',
        method: 'post',
        data: data
    })
}

export const getHisLog = (taskId:string, batchId:number) => {
    return http<Array<string>>({
        url: '/task/hisLog',
        method: 'get',
        params: { taskId : taskId, batchId: batchId }
    })
}

export const getBranchList = (taskId: string) => {
    return http<Array<string>>({
        url: '/task/branchList',
        method: 'get',
        params: { taskId : taskId }
    })
}

export interface TaskTemplateParam{
    name: string,
    flowData: string
}

export interface TaskTemplate{
    id: number,
    name: string,
    flowData: string,
    createTime: string,
}

export const getTaskTemplateList = () => {
    return http<Array<TaskTemplate>>({
        url: '/task/templateList',
        method: 'get'
    })
}

export const saveTaskTemplate = (data: TaskTemplateParam) => {
    return http({
        url: '/task/saveTemplate',
        method: 'post',
        data: data
    })
}

export const deleteTaskTemplate = (id: number) => {
    return http({
        url: '/task/deleteTemplate',
        method: 'delete',
        params: { id: id },
    })
}